﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

public partial class updateuserdata : System.Web.UI.Page
{

    private string str;
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection sc4 = new SqlConnection(Application["sqlconnection"].ToString());
        sc4.Open();
        SqlCommand com_getmajor = new SqlCommand("select Name from Major",sc4);
        SqlDataReader sdrr=com_getmajor.ExecuteReader();
        while (sdrr.Read())
        {
            DropDownList1.Items.Add(sdrr["Name"].ToString());

        }

        sc4.Close();
        TextBox1.Text = Application["password"].ToString();
        str=Application["Email"].ToString();
        while (str[str.Length - 1].Equals(' ')==true)
        {
            str = str.Remove(str.Length - 1);
            //MessageBox.Show(str.Length.ToString());
        }

        TextBox2.Text = str;

        string str_maj=Application["Major"].ToString();
        while (str_maj[str_maj.Length - 1].Equals(' ') == true)
            str_maj = str_maj.Remove(str_maj.Length - 1);
        

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string pass_new = TextBox1.Text;
        string emaa_new = TextBox2.Text;
        string majo_new = DropDownList1.SelectedValue;
        string name_tmp = Application["user_name"].ToString();
        bool flag = false;
        //新资料写入数据库
        SqlConnection sc1 = new SqlConnection(Application["sqlconnection"].ToString());
        sc1.Open();
        SqlCommand com_change = sc1.CreateCommand();
        com_change.CommandText = "update Users set Password=@password where Name=@name";
        com_change.Parameters.Add("@name",SqlDbType.Text);
        com_change.Parameters.Add("@password",SqlDbType.Text);
        com_change.Parameters["@name"]=new SqlParameter("@name",name_tmp);

        if (pass_new.Length == 7 && pass_new.Equals(Application["password"].ToString()) == false)
        {
            com_change.Parameters["@password"] = new SqlParameter("@password", pass_new);
            int count = com_change.ExecuteNonQuery();
            if (count == 1)
                flag = true;
        }
        else Label4.Text = "密码长度错误或与原密码相同！";

        SqlCommand com2 = sc1.CreateCommand();
        com2.CommandText = "update Users set Email=@email,Major=@major where Name=@name";
        com2.Parameters.Add("@name", SqlDbType.Text);
        com2.Parameters.Add("@email", SqlDbType.Text);
        com2.Parameters.Add("@major", SqlDbType.Text);
        com2.Parameters["@name"] = new SqlParameter("@name", name_tmp);
        
        if (emaa_new.Equals(str) == false || majo_new.Equals(Application["Major"].ToString()) == false)
        {
            com2.Parameters["@email"] = new SqlParameter("@email",emaa_new);
            com2.Parameters["@major"] = new SqlParameter("@major", majo_new);
            int count1 = com2.ExecuteNonQuery();
            if (count1 == 1)
            {
                //MessageBox.Show("new email and major is"+emaa_new+majo_new);
                flag = true;
            }
        }
        sc1.Close();

        //if (flag == true)
            //Response.Write("<script language=javascript>alert=('修改成功！')</script>");

        //else Response.Write("<script language=javascript>alert=('没有进行修改~！')</script>"); 
        Application["password"] = TextBox1.Text;
        Application["Email"] = TextBox2.Text;
        Application["Major"] = DropDownList1.Text;
        Response.Redirect("manage.aspx", true);
    
    
    }
}